<div class="Singleform">
<script>
var current_module = '{{$current_model}}';
var current_id = '{{$id}}';
var current_data = null;
{{if $current_data}}
current_data = {{$current_data}};
{{/if}}
</script>
{{if $copy}}
<div class="alert alert-warning">
复制数据，保存时，创建一条新的数据记录。修改不影响原记录内容。
</div>
{{/if}}

<p><a target="_blank" href="/admin/{{$current_controller}}/list"><i class="fa fa-angle-double-left"></i> &nbsp; 返回数据列表</a></p>
<hr>

<div class="tabs">
{{if $current_action=='admin_add'}}
<?php
echo $this->MForm->form(array(    
	'model'=> $current_model,
	'auto_form' => 1,
	'class'=>'form-horizontal form',
	'form_type' => 'add',
	'url' => array('action' => 'add'),
));
?>
{{elseif $current_action=='admin_edit'}}
<?php 
echo $this->MForm->form(array(
     'model'=> $current_model,  
	'class'=>'form-horizontal form',  
    'auto_form' => 1,
    'form_type' => 'edit',
    'url' => array('action' => 'edit',$id,$copy),
));
?>

{{/if}}
</div>



</div>


<form action="">
<input type="file" name="file" id="files" /> <!-- multiple -->
</form>
<div id="upload-progress" class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
    <span class="sr-only">0% Complete</span>
  </div>
</div>
<p><br/></p><p><br/></p><p><br/></p>

<script src="/js/aliyun/aliyun-sdk.min.js"></script>
<script src="/js/aliyun/vod-sdk-upload-1.0.6.min.js"></script>
<script>


$(function(){

	var vid = '';
	var userData = '{"Vod":{"UserData":"{"IsShowWaterMark":"true","Priority":"7"}"}}';

	var vodAuth = {};

	var uploader = new VODUpload({
	    // 开始上传
	    'onUploadstarted': function (uploadInfo) {
	    	console.log('onUploadstarted');
	      console.log(uploadInfo);console.log(vodAuth);
	      //uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress);
	      uploader.setUploadAuthAndAddress(uploadInfo, vodAuth.UploadAuth, vodAuth.UploadAddress);
	    },
	    // 文件上传成功
	    'onUploadSucceed': function (uploadInfo) {      
	      console.log('onUploadSucceed');
	      $('#VodVideoId').val(vid);
	      showSuccessMessage('视频已成功上传');
	      console.log(uploadInfo);
	    },
	    // 文件上传失败
	    'onUploadFailed': function (uploadInfo, code, message) {
	      console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
	      showErrorMessage("code:" + code + ", message:" + message);
	    },
	    // 文件上传进度，单位：字节
	    'onUploadProgress': function (uploadInfo, totalSize, uploadedSize) {
	        console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(uploadedSize * 100 / totalSize) + "%");
	        var percent = Math.ceil(uploadedSize * 100 / totalSize) + "%";
	        $('#upload-progress .progress-bar').show().css('width',percent);
	        $('#upload-progress .sr-only').html( percent + ' Complete');
	    },
	    // 上传凭证超时
	    'onUploadTokenExpired': function () {
	        console.log("onUploadTokenExpired");
	        // uploader.resumeUploadWithAuth(uploadAuth);
	        ajaxAction('/admin/vods/refreshVodUploadAuth/'+vid,null,null,function(request){
	        	console.log(request);
	       		uploader.resumeUploadWithAuth(request.UploadAuth);
	       	});
	    }
	});
	uploader.init();

	document.getElementById("files").addEventListener('change', function (event) {
	    for(var i=0; i<event.target.files.length; i++) {
	       	console.log(event.target.files[i]);
	       	
	       	var postdata = {};
	       	postdata.filename = event.target.files[i].name;
	       	postdata.size = event.target.files[i].size;
	       	
	       	if( $('#VodName').val() != "" ) {
	       		postdata.title = $('#VodName').val();
	       	}
	       	else{
	       		$('#VodName').val( postdata.filename );
	       	}
	       	$('#VodSize').val( postdata.size );

	       	console.log(postdata);
	       	uploader.addFile(event.target.files[i], null, null, null, userData);
	       	ajaxAction('/admin/vods/getVodUploadAuth',postdata,null,function(request){
	       		console.log(request);
	       		vid = request.VideoId;
	       		vodAuth = request;
	       		// 逻辑代码        	
	        	uploader.startUpload();
	        	console.log("call start.");
	        	console.log(uploader.listFiles());
	       	});
	         
	       }
	    });

})

</script>